<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\validate\AuthAdd;
use app\admin\validate\AuthUpdate;
use think\exception\HttpException;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;
use think\Request;

class Auth extends Base
{
    /**
     * 权限菜单列表展示
     * @param \app\admin\model\Auth $auth
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function authList(\app\admin\model\Auth $auth)
    {
        $data = $auth->select();
        View::assign('list',$data);
        return View::fetch('admin-permission');
    }

    /**
     * 菜单权限添加页面展示
     * @param \app\admin\model\Auth $auth
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function authAdd(\app\admin\model\Auth $auth)
    {
        $pid = 0;
        if (request()->param('pid')) {
            $pid = request()->param('pid');
        }
        $data = $auth->where('pid',$pid)->select();
        View::assign('id',$pid);
        View::assign('p_auth',$data);
        return View::fetch('admin-permission-add');
    }

    /**
     * 权限添加
     * @param \app\admin\model\Auth $auth
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function authSave(\app\admin\model\Auth $auth)
    {
        try {
            $data = request()->all();
            validate(AuthAdd::class)->check($data);
            if ($data['id'] == 0){
                if ($data['pid'] == 0){
                    $data['pid_path'] = 0;
                    $data['level'] = 0;
                } else{
                    $obj = $auth->find($data['pid']);
                    $data['pid_path'] = $obj['pid_path'].'_'.$data['pid'];
                    $data['level'] = 1;
                }
            } else {
                if ($data['pid'] == 0){
                    $data['pid'] = $data['id'];
                    $obj = $auth->find($data['id']);
                    $data['pid_path'] = $obj['pid_path'].'_'.$data['id'];
                } else {
                    $obj = $auth->find($data['pid']);
                    $data['pid_path'] = $obj['pid_path'].'_'.$data['pid'];
                }
                $num = count(explode('_',$data['pid_path']));
                $data['level'] = $num - 1;
            }
            $data['sort'] = 50;
            $data['create_time'] = time();
            $data['update_time'] = time();
            unset($data['id']);
            $result = $auth->insert($data);
            if ($result){
                abort(200,'权限添加成功');
            }
            abort(2002,'网络故障，添加失败');
        } catch (HttpException $exception){
            return success($exception->getStatusCode(),$exception->getMessage());
        } catch (ValidateException $exception){
            return success(2001,$exception->getError());
        }
    }

    /**
     * 删除权限
     * 检测传参是否正确
     * @param $id
     * @return \think\response\Json|void
     * @throws \think\db\exception\DbException
     */
    public function authDelete($id){
        try {
            if (!is_numeric($id)){
                abort(2001,'权限传参错误');
            }
            Db::table('pyg_auth')->where('id',$id)
                ->useSoftDelete('delete_time',time())
                ->delete();
            abort(200,'删除成功');
        } catch (HttpException $exception){
            return success($exception->getStatusCode(),$exception->getMessage());
        }
    }

    /**
     * 权限信息编辑页面展示
     * @param \app\admin\model\Auth $auth
     * @param $id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function authEdit(\app\admin\model\Auth $auth,$id){
        $data = $auth->find($id);
        if ($data['level'] == 0){
            $p_auth = null;
        } else {
            $obj = $auth->find($data['pid']);
            $p_auth = $auth->where('pid',$obj['pid'])->select();
        }
        View::assign('info',$data);
        View::assign('p_auth',$p_auth);
        return View::fetch('admin-permission-edit');
    }

    /**
     * 权限信息编辑修改
     * @param \app\admin\model\Auth $auth
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function authUpdate(\app\admin\model\Auth $auth){
        try {
            $data = request()->all();
            validate(AuthUpdate::class)->check($data);
            $obj = $auth->find($data['id']);
            $arr = explode('_',$obj['pid_path']);
            array_pop($arr);
            if ($data['pid'] == 0 && $obj['pid'] != 0){
                $data['pid'] = end($arr);
                $data['level'] = count($arr) - 1;
            } elseif ($data['pid'] != 0 && $obj['pid'] != 0){
                array_push($arr,$data['pid']);
            }
            $data['pid_path'] = implode('_',$arr);
            $data['create_time'] = time();
            $result = $auth->where('id',$data['id'])->update($data);
            if ($result){
                abort(200,'修改成功');
            }
            abort(2002,'修改失败');
        } catch (HttpException $exception){
            return success($exception->getStatusCode(),$exception->getMessage());
        } catch (ValidateException $exception){
            return success(2001,$exception->getError());
        }
    }
}
